<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/sys-admin/index' }" style="size: 16px">
                <i class="el-icon-s-promotion"></i>
                后台管理
            </el-breadcrumb-item>
            <el-breadcrumb-item>添加属性模板</el-breadcrumb-item>
        </el-breadcrumb>

        <el-divider></el-divider>

        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="名称" prop="name">
                <el-input v-model="ruleForm.name" ></el-input>
            </el-form-item>
            <el-form-item label="拼音" prop="pinyin">
                <el-input v-model="ruleForm.pinyin" ></el-input>
            </el-form-item>
            <el-form-item label="关键字" prop="keywords">
                <el-input v-model="ruleForm.keywords" ></el-input>
            </el-form-item>
            <el-form-item label="排序序号" prop="sort">
                <el-input v-model="ruleForm.sort" ></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {

        data() {
            return {
                ruleForm: {
                    name: '测试属性模板001',
                    pinyin: 'PinYin',
                    keywords: '测试属性模板的简介001',
                    sort: '99'
                },
                rules: {
                    name: [
                        { required: true, message: '请输入属性模板名称', trigger: 'blur' },
                        { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
                    ],
                    pinyin: [
                        { required: true, message: '请输属性模板描述', trigger: 'blur' },
                        { pattern:/^[a-zA-Z]{2,35}$/, message: '长度在 2 到 35 个拼音字母', trigger: 'blur' }
                    ],
                    keywords: [
                        { required: true, message: '请输属性模板描述', trigger: 'blur' },
                        { min: 2, max: 35, message: '长度在 2 到 35 个字符', trigger: 'blur' }
                    ],
                    sort: [
                        { required: true, message: '请输入排序序号', trigger: 'blur' },
                        { pattern:/^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是 0~99 之间的数字', trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let url="http://localhost:9080/attribute-template/add-new"
                        console.log('url='+url);
                        let fromData=this.qs.stringify(this.ruleForm);
                        console.log('fromData='+fromData);

                        this.axios.post(url,fromData).then((response)=>{
                            let responseBody=response.data;
                            console.log(responseBody);
                            if(responseBody.state==20000){
                                this.$message({
                                    showClose: true,
                                    message: '属性模板添加成功!',
                                    type: 'success'
                                });
                                this.resetForm(formName);
                            }else{
                                this.$message({
                                    showClose: true,
                                    message: responseBody.message,
                                    type: 'error'
                                });
                            }
                        });
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }

</script>

<style scoped>

</style>